layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
  var curr_page = 0;
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/admin/goods/stocklosslist.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
			var urls = '';
			curr_page = res.current_page;
			
			for (i in res.data) {
				res.data[i]['reason'] = "<a title='正常返回门店' rid='"+res.data[i]['stock_id']+"' href='javascript:;' class='huifu'><i class='layui-icon'>&#xe698;</i></a>&nbsp;&nbsp;";
				res.data[i]['reason'] += "<a title='退还'  rid='"+res.data[i]['stock_id']+"' href='javascript:;' class='fanhui'><i class='layui-icon'>&#xe63c;</i></a>&nbsp;&nbsp;";
			}
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: [[
		  {type: 'checkbox', fixed: 'left'}
		  ,{field:'stock_id', title: '序号'}
		  ,{field:'goods_name', title: '产品名称'} 
		  ,{field:'news_name', title: '异常原因'}
		  ,{field:'number', title: '数量'}
		  ,{field:'unit', title: '单位'}
		  ,{field:'imei', title: 'IMEI'}
		  ,{field:'sn', title: 'S/N'}
		  ,{field:'period_time', title: '保质期'}
		  ,{field:'buying_price', title: '进货价（元）'}
		  ,{field:'contract_sn', title: '合同编号'}
		  ,{field:'supplier_id', title: '供应商'}
		  ,{field:'shop_id', title: '所属门店'}
		  ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		  ,curr:curr_page
		}
		,response: {
			statusCode: 10001
		}
	  });
	}
	tableInit();
	 $('body').on('click', '.huifu', function(){
		var get_this = $(this);
		parent.layer.confirm('你确定要返回门店吗?', function(){
			var params = {};
			params.ids = get_this.attr('rid');
			$.ajax({
				type: "POST",
				url: "/admin/goods/returnstock.html",
				cache: false, //禁用缓存
				data: params, //传入组装的参数
				dataType: "json",
				success: function (result) {
					if(result.code==10001){
						tableInit();
						parent.layer.msg('操作成功', {icon: 1});
					}else{
						parent.layer.msg(result.msg, {icon: 5});
					}
				},
				error:function () {
					parent.layer.msg('网络错误', {icon: 5});
				}
			})
		});
	}); 
	$('body').on('click', '.fanhui', function(){
		var get_this = $(this);
		parent.layer.confirm('你确定要退还吗?', function(){
			var params = {};
			params.ids = get_this.attr('rid');
			$.ajax({
				type: "POST",
				url: "/admin/goods/recovery.html",
				cache: false, //禁用缓存
				data: params, //传入组装的参数
				dataType: "json",
				success: function (result) {
					if(result.code==10001){
						tableInit();
						parent.layer.msg('操作成功', {icon: 1});
					}else{
						parent.layer.msg(result.msg, {icon: 5});
					}
				},
				error:function () {
					parent.layer.msg('网络错误', {icon: 5});
				}
			})
		});
	}); 
	//监听提交
	form.on('submit(sreach)',
	function(data) {
		tableInit(data.field);
		return false;
	});
	$('body').on('click', '#getCheckReturn', function(){
	parent.layer.confirm('你确定要批量退还吗?', function(){
		//获取选中数据
      var checkStatus = table.checkStatus('dataTables-example')
      ,data = checkStatus.data;
	  console.log(data);
	  if(data.length == 0)
	  {
		  parent.layer.msg('未选择数据', {icon: 5});
		  return false;
	  }
	  var ids = '';
	  for (i in data) 
	  {
		ids += data[i].stock_id + ',';
	  }
	  var params = {};
	  params.ids=ids.substring(0,ids.length-1);
	  $.ajax({
			type: "POST",
			url: "/admin/goods/recovery.html",
			cache: false, //禁用缓存
			data: params, //传入组装的参数
			dataType: "json",
			success: function (result) {
				if(result.code==10001){
					tableInit();
					parent.layer.msg('操作成功', {icon: 1});
				}else{
					parent.layer.msg(result.msg, {icon: 5});
				}
			},
			error:function () {
				parent.layer.msg('网络错误', {icon: 5});
			}
		})
	  })
	});
});

